USE [BSM]
GO
/****** Object:  View [dbo].[VW_Vouchers]    Script Date: 11/03/2019 13:24:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[VW_Vouchers]
AS
SELECT     dbo.VoucherDetails.VoucherID, dbo.Voucher.VoucherDate, dbo.Voucher.VoucherNo, dbo.Voucher.SessionID, dbo.SessionInfo.SessionTitle, 
                      dbo.SessionInfo.SessionFrom, dbo.SessionInfo.SessionTo, dbo.SessionInfo.CurrentSession, dbo.Voucher.Remarks AS MasterRemarks, dbo.Voucher.ReadOnly, 
                      dbo.VoucherDetails.VoucherDetailID, dbo.VoucherDetails.AccountNo, dbo.Accounts.AccountTitle, dbo.VoucherDetails.Remarks, 
                      CASE WHEN (dbo.VoucherDetails.DrOrCr = N'Dr') THEN dbo.VoucherDetails.Amount ELSE - 1 * dbo.VoucherDetails.Amount END AS Amount, 
                      CASE WHEN (dbo.VoucherDetails.DrOrCr = N'Dr') THEN dbo.VoucherDetails.Amount ELSE 0 END AS Debit, CASE WHEN (dbo.VoucherDetails.DrOrCr = N'Cr') 
                      THEN dbo.VoucherDetails.Amount ELSE 0 END AS Credit, dbo.VoucherDetails.DrOrCr, dbo.Accounts.ParentAccount, dbo.Voucher.BankAccountID, dbo.Voucher.LoginID, 
                      dbo.Employees.EmployeeName, dbo.Voucher.HostName, dbo.Voucher.EntryDateTime, Accounts_2.AccountTitle AS BankAccountTitle, 
                      dbo.VoucherDetails.Amount AS ActualAmount, dbo.Voucher.Expense, dbo.Voucher.Discount, dbo.Voucher.ModifyID, Employees_1.EmployeeName AS ModifyName, 
                      dbo.Voucher.ModifyHostName, dbo.Voucher.ModifyDateTime, dbo.VoucherDetails.ItemID, dbo.vw_Items.ItemCode, dbo.vw_Items.ItemName, 
                      dbo.vw_Items.ItemNameUrdu, dbo.VoucherDetails.Qty, dbo.VoucherDetails.Rate, dbo.vw_Items.GroupID, dbo.vw_Items.GroupName, dbo.Voucher.CustomerName, 
                      dbo.VoucherDetails.ComPercentD, dbo.VoucherDetails.ComAmountD, dbo.VoucherDetails.TotalAmountD, dbo.VoucherDetails.LotNo, 
                      dbo.Accounts.Remarks AS CustomerAddress, dbo.Accounts.CellNo, dbo.Accounts.Phone, dbo.Accounts.NameUrdu, dbo.Voucher.GroupID AS SubPartyID, 
                      ISNULL(dbo.Groups.GroupName, '.') AS SubPartyName, dbo.Accounts.AreaID
FROM         dbo.vw_Items RIGHT OUTER JOIN
                      dbo.VoucherDetails ON dbo.vw_Items.ItemID = dbo.VoucherDetails.ItemID LEFT OUTER JOIN
                      dbo.Accounts ON dbo.VoucherDetails.AccountNo = dbo.Accounts.AccountNo LEFT OUTER JOIN
                      dbo.Employees AS Employees_1 RIGHT OUTER JOIN
                      dbo.Accounts AS Accounts_2 RIGHT OUTER JOIN
                      dbo.Groups RIGHT OUTER JOIN
                      dbo.Voucher ON dbo.Groups.GroupID = dbo.Voucher.GroupID ON Accounts_2.AccountNo = dbo.Voucher.BankAccountID ON 
                      Employees_1.EmployeeID = dbo.Voucher.ModifyID LEFT OUTER JOIN
                      dbo.Employees ON dbo.Voucher.LoginID = dbo.Employees.EmployeeID ON dbo.VoucherDetails.VoucherID = dbo.Voucher.VoucherID LEFT OUTER JOIN
                      dbo.SessionInfo ON dbo.Voucher.SessionID = dbo.SessionInfo.SessionID
